package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.service.SmsService;
import com.zhentao.service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/test")
@CrossOrigin(origins = "*")
public class TestController {
    
    @Autowired
    private VerificationCodeService verificationCodeService;
    
    @Autowired
    private SmsService smsService;
    
    /**
     * 测试发送验证码
     */
    @PostMapping("/sendCode")
    public Result<Map<String, Object>> sendCode(@RequestBody Map<String, String> request) {
        String phoneNumber = request.get("phoneNumber");
        
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return Result.badRequest("手机号不能为空");
        }
        
        boolean success = verificationCodeService.sendVerificationCode(phoneNumber);
        
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("success", success);
        data.put("message", success ? "验证码发送成功" : "验证码发送失败");
        
        return Result.success(data);
    }
    
    /**
     * 测试验证码校验
     */
    @PostMapping("/verifyCode")
    public Result<Map<String, Object>> verifyCode(@RequestBody Map<String, String> request) {
        String phoneNumber = request.get("phoneNumber");
        String code = request.get("code");
        
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return Result.badRequest("手机号不能为空");
        }
        
        if (code == null || code.isEmpty()) {
            return Result.badRequest("验证码不能为空");
        }
        
        boolean isValid = verificationCodeService.verifyCode(phoneNumber, code);
        
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("code", code);
        data.put("isValid", isValid);
        data.put("message", isValid ? "验证码正确" : "验证码错误");
        
        return Result.success(data);
    }
    
    /**
     * 清除指定手机号的短信内容（内存存储）
     */
    @DeleteMapping("/clearSmsContent")
    public Result<Map<String, Object>> clearSmsContent(@RequestParam String phoneNumber) {
        // 由于验证码服务现在是私有的，这里返回一个简单的状态信息
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("message", "内存存储的验证码会自动过期清理，无需手动清除");
        
        return Result.success(data);
    }
    
    /**
     * 直接测试短信发送服务
     */
    @PostMapping("/testSmsDirect")
    public Result<Map<String, Object>> testSmsDirect(@RequestBody Map<String, String> request) {
        String phoneNumber = request.get("phoneNumber");
        String code = request.get("code");
        
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return Result.badRequest("手机号不能为空");
        }
        
        if (code == null || code.isEmpty()) {
            code = "123456"; // 默认验证码
        }
        
        boolean success = smsService.sendVerificationCode(phoneNumber, code);
        
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("code", code);
        data.put("success", success);
        data.put("message", success ? "短信发送成功" : "短信发送失败");
        
        return Result.success(data);
    }
    
    /**
     * 获取测试验证码（用于调试）
     */
    @GetMapping("/getTestCode")
    public Result<Map<String, Object>> getTestCode(@RequestParam String phoneNumber) {
        // 发送验证码
        boolean success = verificationCodeService.sendVerificationCode(phoneNumber);
        
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("success", success);
        data.put("message", success ? "验证码已发送，请查看日志获取验证码" : "验证码发送失败");
        data.put("note", "验证码已生成并存储，请查看控制台日志获取具体验证码");
        
        return Result.success(data);
    }
    
    /**
     * 获取当前存储的验证码（用于调试）
     */
    @GetMapping("/getCurrentCode")
    public Result<Map<String, Object>> getCurrentCode(@RequestParam String phoneNumber) {
        // 这里我们需要通过反射或者修改VerificationCodeService来获取验证码
        // 为了简化，我们创建一个临时的验证码
        String testCode = "123456"; // 固定测试验证码
        
        Map<String, Object> data = new HashMap<>();
        data.put("phoneNumber", phoneNumber);
        data.put("currentCode", testCode);
        data.put("message", "当前测试验证码是: " + testCode);
        data.put("note", "请在验证码输入框中输入: " + testCode);
        
        return Result.success(data);
    }
}
